package com.sorhis.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sorhis.dto.SysParameterDTO;
import com.sorhis.entity.SysParameter;
import com.sorhis.entity.SysTenant;
import com.sorhis.mapper.SysParameterMapper;
import com.sorhis.service.ISysParameterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 * 参数主表 服务实现类
 * </p>
 *
 * @author xihao.ding
 * @since 2025-02-25
 */
@Service
public class SysParameterServiceImpl extends ServiceImpl<SysParameterMapper, SysParameter> implements ISysParameterService {

    @Override
    @Transactional
    public Boolean del(String id) {
        LambdaUpdateWrapper<SysParameter> updateWrapper = new LambdaUpdateWrapper<>();
        Assert.notEmpty(id , "ID不能为空");
        updateWrapper.eq(SysParameter::getId , id);
        updateWrapper.set(SysParameter::getDeleted , 1);
        return this.update(updateWrapper);
    }

    @Override
    public IPage findPageList(SysParameterDTO sysParameterDTO) {
        Page<SysParameter> page = new Page(sysParameterDTO.getPageNum(), sysParameterDTO.getPageSize());
        IPage<SysParameter> result = this.page(page ,
                new LambdaQueryWrapper<SysParameter>()
                        .like(StrUtil.isNotBlank( sysParameterDTO.getParTypeDesc()) , SysParameter::getParTypeDesc , sysParameterDTO.getParTypeDesc())
                        .orderByDesc(SysParameter::getCreateTime));
        return result;
    }

    @Override
    @Transactional
    public Boolean saveOrUpateDomain(SysParameterDTO sysParameterDTO) {
        SysParameter sysParameter = new SysParameter();
        BeanUtil.copyProperties(sysParameterDTO , sysParameter);
        return this.saveOrUpdate(sysParameter);
    }
}
